<template>
  <div>
    <ul class="list">
      <li class="item" 
        v-for="item in letters" 
        :key="item"
        :ref="item"
        @touchstart="handleTouchStart" 
        @touchmove="handleTouchMove" 
        @touchend="handleTouchEnd" 
        @click="handleLetterClick">
        {{item}}
      </li>
    </ul>
  </div>
</template>

<script>
  export default {
    name: 'cityAlphabet',
    props: {
      cities: Object
    },
    computed: {
      letters () {
        const letters = []
        for (let i in this.cities) {
          letters.push(i)
        }
        return letters
      }
    },
    data () {
      return {
        touchStatus: false,
        startY: 0
      }
    },
    updated () {
      this.startY = this.$refs['A'][0].offsetTop
    },
    methods: {
      handleLetterClick (a) {
        this.$emit('change', a.target.innerText)
      },
      handleTouchStart () {
        this.touchStatus = true
      },
      handleTouchMove (a) {
        if (this.touchStatus) {
          const touchY = a.touches[0].clientY - 79
          const index = Math.floor((touchY - this.startY) / 20)
          if (index >= 0 && index <= this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }  
      },
      handleTouchEnd () {
        this.touchStatus = false
      }
    }
  }
</script>

<style lang="stylus" scoped>
  .list
    display: flex
    flex-direction: column
    justify-content: center
    position: absolute
    top: 1.58rem
    right: .2rem
    bottom: 0
    width: .4rem
    .item
      line-height: .4rem
      text-align: center
      color: #00bcd4
</style>
